package com.qifan.energy.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.bean.vo.RegisterVO;
import com.qifan.energy.R;
import com.qifan.energy.global.Constant;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ActivityStackUtil;
import com.qifan.energy.util.IntentUtils;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.util.count.CountDownTimerUtil;
import com.qifan.energy.widget.dialog.LoadingUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class SignUpActivity extends BaseActivity {

    @BindView(R.id.done_btn)
    TextView doneBtn;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.nickname_et)
    EditText nickNameEt;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.pwd_et)
    EditText pwdEt;
    @BindView(R.id.register_code_et)
    EditText registerCodeEt;

    private boolean isPwdVisible = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sign_up;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        /* 更改状态栏 */
        changeStatusTransparentFontBlack();
    }

    @Override
    protected void initData() {
        /*UUID deviceUuid = new DeviceUuidFactory(UIUtils.getContext()).getDeviceUuid();
        Logger.i("deviceUuid:" + deviceUuid);*/
    }

    @OnClick(R.id.code_tv)
    void onCodeTvClicked(TextView tv){
        String phone = phoneEt.getText().toString();
        if (TextUtils.isEmpty(phone)){
            ToastUtils.showShort("手机号不能为空");
            return;
        } else {
            if (phone.length() != 11){
                ToastUtils.showShort("手机号格式不对哦~");
                return;
            }
            tv.setEnabled(false);
            LoadingUtils.show(this);
            NetUtils.getAuthCode(phone, new MyObserver<String>(this) {
                @Override
                public void onSuccess(String result) {
                    tv.setEnabled(true);
                    LoadingUtils.hide();
                    Logger.i("sso/getAuthCode:" + result);

                    /* 开始倒计时 */
                    CountDownTimerUtil mCountDownTimer = new CountDownTimerUtil(tv, 60000, 1000);
                    mCountDownTimer.start();
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    tv.setEnabled(true);
                    LoadingUtils.hide();
                    ToastUtils.showShort(errorMsg);
                }
            });
        }
    }

    @OnClick({R.id.user_protocol_tv,R.id.user_privacy_tv})
    void onProtocolTvClicked(TextView textView){
        switch (textView.getId()) {
            case R.id.user_protocol_tv:
                IntentUtils.startWebsite(this, Constant.APPSTATIC_SERVICE,"用户协议",true);
                break;
            case R.id.user_privacy_tv:
                IntentUtils.startWebsite(this,Constant.PRIVATE_HTTP,"隐私协议",true);
                break;
        }
    }

    @OnClick(R.id.done_btn)
    void onDoneBtnClicked(){
        String phone = phoneEt.getText().toString().trim();
        String nickname = nickNameEt.getText().toString().trim();
        String code = codeEt.getText().toString().trim();
        String pwd = pwdEt.getText().toString().trim();
        String registerCode = registerCodeEt.getText().toString().trim();
        if (TextUtils.isEmpty(phone)){
            ToastUtils.showShort("手机号不能为空哦~");
            return;
        }
        if (TextUtils.isEmpty(nickname)){
            ToastUtils.showShort("昵称不能为空哦~");
            return;
        }
        if (TextUtils.isEmpty(code)){
            ToastUtils.showShort("验证码不能为空哦~");
            return;
        }
        if (TextUtils.isEmpty(pwd)){
            ToastUtils.showShort("密码不能为空哦~");
            return;
        }
        if (TextUtils.isEmpty(registerCode)){
            ToastUtils.showShort("邀请码必填哦~");
            return;
        }

        doneBtn.setEnabled(false);
        LoadingUtils.show(this);
        RegisterVO registerVO = new RegisterVO();
        registerVO.setNickname(nickname);
        registerVO.setUsername(phone);
        registerVO.setTelephone(phone);
        registerVO.setAuthCode(code);
        registerVO.setPassword(pwd);
        registerVO.setRegisterCode(registerCode);
        NetUtils.regist(registerVO, new MyObserver<String>(this) {
            @Override
            public void onSuccess(String result) {
                doneBtn.setEnabled(true);
                LoadingUtils.hide();
                ToastUtils.showShort("注册成功!");
                //返回上级页面
                ActivityStackUtil.finishTopActivity();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                doneBtn.setEnabled(true);
                LoadingUtils.hide();
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    @OnClick(R.id.password_eye_iv)
    void onPasswordEyeIvClicked(ImageView imageView){
        isPwdVisible = !isPwdVisible;
        if (isPwdVisible){
            imageView.setImageResource(R.mipmap.ic_edit_eye_open_gray);
            pwdEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            imageView.setImageResource(R.mipmap.ic_edit_eye_close_gray);
            pwdEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        pwdEt.setSelection(pwdEt.getText().length());
    }
}
